import {
  CALL,
  SENDMESSAGE,
  CHAT,
  CHAT_REQUEST,
  CHAT_SUCCESS,
  CHAT_FAILURE,
  CLOSE,
  INIT
} from "./actionTypes";

import { updateObject } from "@utils/makeReducerCreator";
import * as websocket from "@utils/vedio";
export const initialState = {
  addressList: {
    loading: false
  },
  chatRoom: {
    loading: false,
    chat: {},
    vedio: {}
  }
};

function init(state, action) {
  websocket.setVedio(
    action.data.selfVedio.current,
    action.data.otherVedio.current
  );
  websocket.register(action.data.username);
  // setTimeout(() => {
  //   websocket.register("2323");
  // }, 5000);
  // setTimeout(() => {
  //   websocket.call("a");
  // }, 5000);
  // console.log("注册成功");
  return updateObject(state, {
    chatRoom: {
      ...state.chatRoom,
      vedio: action.data
    }
  });
}
function close(state, action) {
  websocket.destory();
  console.log("结束成功");
  return updateObject(state, {
    chatRoom: {
      ...state.chatRoom,
      vedio: action.data
    }
  });
}
export function home(state = initialState, action) {
  switch (action.type) {
    case INIT:
      return init(state, action);
    case CLOSE:
      return close(state, action);
    default:
      return state;
  }
}
